// pages/health-certificate-info/health-certificate-info.js
import user from '../../utils/user'
import global from '../../utils/global'
import {
    register,
    distinguishIdCard, // 实名认证 点击身份证识别身份证信息
    getEcho, // 用户信息回显
    encryptedPhoneData
} from '../../api/app.js'
import {
    wxfileTobase64
} from '../../utils/util'
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        healthimg: "",
        img: "",
        dataInformation: {},
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getEcho()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },
    async uploadhealthcard() {



        wx.navigateTo({
            url: '/pages/demo/demo?type=' + '4',
            events: {
                healthCard: async (data) => {
                    console.log(data, 'dufihgiyd');
                    this.setData({
                        img: data.url
                    })
                }
            }
        })


        // console.log('上传健康证');
        // wx.chooseImage({
        //     success:(res)=> {
        //         const tempFilePaths = res.tempFilePaths
        //         console.log(tempFilePaths);
        //         this.setData({
        //             img: tempFilePaths[0]
        //         })
        //     }
        // })
    },
    async getEcho() {
        console.log('调用啦吗')
        const result = await getEcho()
        console.log(result, 'result')
        this.setData({
            dataInformation: result,
        })
    },
    async register(fileUrl) {
        console.log(fileUrl, 'fileUrl')
        this.data.dataInformation.openId = user.openid
        this.data.dataInformation.userHeadUrl = user.avatarUrl
        this.data.dataInformation.bankCardIs = 1
        this.data.dataInformation.userName = this.data.dataInformation.userName.replace(/\s*/g, '')
        this.data.dataInformation.isOpenSalaryCard = Number(this.data.goRelation)
        this.data.dataInformation.healthCard = fileUrl
        console.log(this.data.dataInformation, 'prevPage')
        const result = await register({
            ...this.data.dataInformation,
            checkFlag: 1
        })
        console.log(result, 'result')
        app.toast('提交身份信息成功')
        console.log(app.globalData, 'app.globalData')
        // setTimeout(() => {
        // if (app.globalData.homePath) {
        // wx.redirectTo({
        // url: '/pages/join/join',
        // })
        // } else if (app.globalData.registerBack) {
        // wx.redirectTo({
        // url: app.globalData.registerBack,
        // })
        // app.globalData.registerBack = ''
        // } else {
        // wx.switchTab({
        // url: '/pages/index/index'
        // })
        // }
        // }, 1000)
        setTimeout(() => {
            wx.navigateTo({
              url: '/pages/personal-info/personal-info',
            })
        }, 1000)
    },
    async submithealthcard() {
        console.log(this.data.img, '提交健康证');
        wx.uploadFile({
            url: global.baseUrl + '/api/v1/blue/user/upload',
            filePath: this.data.img,
            name: 'file',
            formData: {
                'user': 'test'
            },
            success: (res) => {
                console.log(res, 'resres')
                const data = res.data
                console.log(JSON.parse(data));
                if (JSON.parse(data).code === 200) {
                    this.register(JSON.parse(data).data.fileUrl)
                    app.toast('上传健康证成功')
                }
            }
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})